function MutiTree()
{
    this.rst = new Array();

}
MutiTree.prototype.parseData =function(data)
{
    for (var i = 0, len = data.length; i < len; i++)
    {
        var item = data[i];
        if (!item.parentId ||item.parentId == "" || item.parentId ==item.id) {
            this.rst.push(item);
        } else {
            var parentId = item.parentId;
            var elem = this.getParent(parentId, this.rst);
            if (elem) {
                elem.chrild ? elem.chrild.push(item) : (elem.chrild = [item]);
            }
        }
    }
}

MutiTree.prototype.getParent = function(parentId, elems) {
    if (!elems) {
        return;
    }
    for (var i = 0, len = elems.length; i < len; i++) {
        var elem = elems[i];
        if (elem.id == parentId) {
            return elem;
        } 
    }
	for (var i = 0, len = elems.length; i < len; i++) {
        var elem = elems[i];
        if(elem.chrild)
                return this.getParent(parentId, elem.chrild);
    }
	
	
}


var treestr = new Array();

function puductTree(rst)
{	
	var inStack = false;
    $.each(rst , function(index, item){
        if(item.url.length>0 && item.url!=''&& item.url!='#')
        {
			treestr.push("<li class=\"treeview\">");
            treestr.push("<a href='"+item.url+"' class=\"treeNode\"  target=\"manContent\"><i class=\"fa\"></i> <span>"+item.name+"</span>"+
                "<span class=\"pull-right-container\"><i class=\"fa fa-angle-left pull-right\"></i></span></a>");
        }else{
			treestr.push("<li class=\"treeview\">");
			treestr.push("<a href='javascript:void(0)'><i class=\"fa \"></i> <span>"+item.name+"</span>"+
                "<span class=\"pull-right-container\"><i class=\"fa fa-angle-left pull-right\"></i></span></a>");
		}


        if(item.chrild)
        {
            treestr.push("<ul class=\"treeview-menu\">");
            puductTree(item.chrild)
            treestr.push("</ul>")
        }

        treestr.push("</li>")
    });
}

$(function(){

    $.ajax({
        type: "GET",
        url: basePath+"/privilege/resource/queryAllMenus.do",
       // data: par,
        dataType: "json",
        async: false,
        success: function(msg) {
            var mutiTree = new MutiTree();
            mutiTree.parseData(msg);
            puductTree(mutiTree.rst);
			if(treestr && treestr.length >0)
			{
				treestr[0] ="<li class=\"active treeview\">";
			}
            var newtree = treestr.join("");
			$(".sidebar-menu").empty().html(newtree);
        }
    });


});
